package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session){
        //接收参数
        String username = employee.getUsername();
        String password = employee.getPassword();

        //交给service判断,用户名是否存在,存在返回查询到的值,不存在则返回null
        employee = employeeService.login(username,password);

        if(employee ==null){
            return R.error("账号或密码错误");
        }
        //5、查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if(employee.getStatus()==0){
            return R.error("账号已禁用");
        }
        //6、登录成功，将员工id存入Session并返回登录成功结果
        session.setAttribute("employee",employee.getId());
        return R.success(employee);
    }

    @PostMapping("/logout")
    public R<Employee> logout(HttpSession session){
        session.invalidate();
        return R.success(null);
    }

    //完成新增员工功能
    @PostMapping()
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee){
        //log.info("获取到的对象为:employee{}",employee);
        String password = "123456";
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        employee.setPassword(password);
//        employee.setCreateTime( LocalDateTime.now());
//        employee.setUpdateTime( LocalDateTime.now());
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("新增员工成功");
    }

    //员工分页查询功能
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize,String name){
        log.info("分页查询请求参数:page:{};pageSize:{};name:{}",page,pageSize,name);

        Page pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //b.调用分页查询方法
        employeeService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    //启用/禁用员工账号
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        //从Session获取当前登录用户的id,处理精度丢失问题
        Long empId = (Long) request.getSession().getAttribute("employee");
        log.info("要修改的员工id为:{}",employee.getId());
//        employee.setUpdateUser(empId);
//        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    //更新功能的数据回显
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable("id") Long id){
        log.info("数据回显所需要查询的id为{}",id);
        Employee employee = employeeService.getById(id);
        if(employee==null){
            return R.error("没有查询到对应员工信息");
        }
        return R.success(employee);
    }

}
